﻿
using System;
using System.Collections.Generic;

namespace GNUGo.Net.CLI.CliControls {
    public class CliForm {
        private readonly List<CliControl> _children = new List<CliControl>();
        private bool _fullScreen;
        private ConsoleColor _forecolor;
        private ConsoleColor _backcolor;
        public event PaintEventHandler Paint;

        public CliForm() {
            _forecolor = ConsoleColor.Gray;
            _backcolor = ConsoleColor.Black;
            Paint += OnPaint;
        }

        public void AddChild(CliControl child) {
            _children.Add(child);
        }

        public virtual void Invalidate() {
            if (Paint != null) {
                CliGraphics g = null;
                if (FullScreen) {
                    g = new CliGraphics(Console.WindowWidth, Console.WindowHeight - 1, Forecolor, Backcolor);
                }
                PaintEventArgs e = new PaintEventArgs(g);
                Paint(this, e);
                g.DrawToScreen(0, 0);
            }
        }

        protected virtual void OnPaint(object sender, PaintEventArgs args) {
            foreach (CliControl child in _children) {
                child.PaintToBuffer(args.Graphics);
            }
        }

        public bool FullScreen {
            get { return _fullScreen; }
            set { _fullScreen = value; Invalidate(); }
        }

        public ConsoleColor Forecolor {
            get { return _forecolor; }
            set {
                if (_forecolor != value) {
                    _forecolor = value;
                    Invalidate();
                }
            }
        }

        public ConsoleColor Backcolor {
            get { return _backcolor; }
            set {
                if (_backcolor != value) {
                    _backcolor = value;
                    Invalidate();
                }
            }
        }
    }
}
